import pygame
import sys
import random
import math

class Ball(pygame.sprite.Sprite):
    def __init__(self,image,position,speed,size):
        pygame.sprite.Sprite.__init__(self)
        
        self.image=pygame.image.load(image).convert_alpha()
        self.rect=self.image.get_rect()
        self.rect.left,self.rect.top=position
        self.speed=speed
        self.width, self.height = size[0], size[1]
        #self.radius 是collide_circle方法中一定要有的半径参数
        self.radius =self.rect.width/2
        
    def move(self):
        self.rect = self.rect.move(self.speed)
        
        if self.rect.right <0:
            self.rect.left=self.width
            
        elif self.rect.left > self.width:
            self.rect.right=0
            
        elif self.rect.bottom <0:
            self.rect.top=self.height
            
        elif self.rect.top > self.height:
            self.rect.bottom=0
            
def checkball(item,target):
    check=[]
    for each in target:
        distance=math.sqrt(math.pow(item.rect.center[0]-each.rect.center[0],2)+\
                           math.pow(item.rect.center[1]-each.rect.center[1],2))
        long = (item.rect.width+each.rect.width)/2
        if distance<=long:
            check.append(each)

    return check
def main():
    pygame.init()
    pygame.mixer.init()

    image='gray_ball.png'
    background='background.png'
    balls=[]
    group=pygame.sprite.Group()
    
    size=width,height=1024,681
    screen=pygame.display.set_mode(size)
    pygame.display.set_caption("Play the ball")
    clock=pygame.time.Clock()
    bg=pygame.image.load(background).convert_alpha()
    pygame.mixer.music.load(bg_music.ogg)
    pygame.mixer.music.set_volume(0.2)
    pygame.mixer.music.play()

    ball_num=5
    for i in range(ball_num):
        position=random.randint(0,width-100),random.randint(0,height-100)
        speed=[random.randint(-10,10),random.randint(-10,10)]
        ball=Ball(image,position,speed,size)
        #checkball是自己写的碰撞检测checkball(ball,balls)
        #pygame自带碰撞检查就是 pygame.sprite.spritelcollide
        while pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
            position=random.randint(0,width-100),random.randint(0,height-100)
            ball=Ball(image,position,speed,size)
            #或者改成
            #ball.rect.left,ball.rect.top=random.randint(0,width-100),random.randint(0,height-100)
        #balls.append(ball)
        group.add(ball)
        
    
    run=True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
                
        screen.blit(bg,(0,0))
        for each in group:
            each.move()
            screen.blit(each.image,each.rect)

        '''for i in range(ball_num):
            item=balls.pop(i)
            if pygame.sprite.spritecollide(item,balls,False,pygame.sprite.collide_circle):
                item.speed[0]=-item.speed[0]
                item.speed[1]=-item.speed[1]
            balls.insert(i,item)'''
        for each in group:
            group.remove(each)
            if pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle):
                each.speed[0]=-each.speed[0]
                each.speed[1]=-each.speed[1]
            group.add(each)
        
        pygame.display.flip()
        clock.tick(120)
        

if __name__=='__main__':
    main()
